/***************************************************************************
 *   Copyright (C) 2009 - 2010 by Simon Qian <SimonQian@SimonQian.com>     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef __VSFIP_NETDRV_H_INCLUDED__
#define __VSFIP_NETDRV_H_INCLUDED__

struct vsfip_netdrv_t;
struct vsfip_link_op_t
{
	vsf_err_t (*init)(struct vsfip_netdrv_t *netdrv);
	vsf_err_t (*fini)(struct vsfip_netdrv_t *netdrv);

	vsf_err_t (*output)(struct vsfip_netdrv_t *netdrv);
	vsf_err_t (*input)(struct vsfip_netdrv_t *netdrv);
	bool (*routable)(struct vsfip_netdrv_t *netdrv, const struct vsfip_ipaddr_t *dest_addr);
};

struct vsfip_netdrv_cb_t
{
	void *param;
	void (*on_connect)(void *param, struct vsfip_netdrv_t *netdrv);
	void (*on_connected)(void *param, struct vsfip_netdrv_t *netdrv);
	void (*on_disconnect)(void *param, struct vsfip_netdrv_t *netdrv);
	void (*on_data)(void *param, uint8_t *buf, uint32_t len, bool out);
};

struct vsfip_netdrv_adapter_t
{
	uint32_t netif_size;
	vsf_err_t (*on_connect)(void *netif);
	void (*on_disconnect)(void *netif);

	void *(*alloc_buf)(struct vsf_buffer_t *buf);
	void (*free_buf)(void *netbuf);

	uint8_t *(*header)(void *netbuf, int32_t len);
	void (*on_outputted)(void *netif, void *netbuf, vsf_err_t err);
	void (*on_inputted)(void *netif, void *netbuf, uint32_t len);
};

struct vsfip_netdrv_netbuf_t
{
	void *netbuf;
	struct vsf_buffer_t buffer;
};

struct vsfip_netdrv_t
{
	void *netif;
	struct vsfip_netdrv_adapter_t *adapter;
	struct vsfip_netdrv_cb_t *cb;
	struct vsfip_macaddr_t macaddr;

	// protected
	void *param;
	const struct vsfip_link_op_t *op;
	uint16_t header_size;
	uint16_t mtu;
	uint16_t hwtype;
	bool tofree;
	bool connected;

	// private
	struct vsfip_netdrv_netbuf_t txbuf, rxbuf;
};

struct vsfip_netdrv_t *vsfip_netdrv_new(uint32_t priv_size,
		const struct vsfip_link_op_t *op, struct vsfip_netdrv_cb_t *cb);
void vsfip_netdrv_free(struct vsfip_netdrv_t *netdrv);

// for netif
vsf_err_t vsfip_netdrv_init(struct vsfip_netdrv_t *netdrv);
vsf_err_t vsfip_netdrv_fini(struct vsfip_netdrv_t *netdrv);
vsf_err_t vsfip_netdrv_output(struct vsfip_netdrv_t *netdrv, void *netbuf,
		uint8_t *ptr, uint32_t len);

// for link driver
uint8_t *vsfip_netdrv_header(struct vsfip_netdrv_t *netdrv, void *netbuf, int32_t len);
vsf_err_t vsfip_netdrv_connect(struct vsfip_netdrv_t *netdrv);
void vsfip_netdrv_disconnect(struct vsfip_netdrv_t *netdrv);
bool vsfip_netdrv_connected(struct vsfip_netdrv_t *netdrv);
void vsfip_netdrv_on_outputted(struct vsfip_netdrv_t *netdrv, vsf_err_t err);
void vsfip_netdrv_on_inputted(struct vsfip_netdrv_t *netdrv, vsf_err_t err);

#endif		// __VSFIP_NETIF_H_INCLUDED__
